#!/bin/bash

SERVICE_NAME=heartbeat-server
PATH_TO_JAR=heartbeat-server-0.1.jar
PID_PATH_NAME=/tmp/heartbeat-sever-pid

JAVA_OPTS="-ms512m -mx512m -Xmn256m -Djava.awt.headless=true -XX:MaxPermSize=128m"
CLASSPATH="."
for i in ./lib/*.jar; do
   CLASSPATH="$CLASSPATH":"$i"
done
CLASSPATH="$CLASSPATH":"$PATH_TO_JAR"
MAIN_CLASS=com.wxtech.heartbeat.App

case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
            nohup java $JAVA_OPTS -classpath $CLASSPATH $MAIN_CLASS &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stoping ..."
            kill $PID;
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill $PID;
            echo "$SERVICE_NAME stopped ...";
            rm $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            nohup java $JAVA_OPTS -classpath $CLASSPATH $MAIN_CLASS &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    test)
	java $JAVA_OPTS -classpath $CLASSPATH $MAIN_CLASS 
esac
